#! /usr/bin/env python

import sys, os

import waflib.Task, waflib.TaskGen, waflib.Options
from waflib.TaskGen import extension
from waf_dynamo import platform_supports_feature

def configure(conf):
    if platform_supports_feature(conf.env.PLATFORM, 'compute', None):
        conf.env.append_unique('DEFINES', 'DM_HAVE_PLATFORM_COMPUTE_SUPPORT')

def build(bld):
    dirs = ['animationset/*',
            'atlas/*',
            'buffer/*',
            'camera/*',
            'collection_factory/*',
            'collection_proxy/*',
            'collision_object/*',
            'convex_shape/*',
            'display_profiles/*',
            'emitter/*',
            'factory/*',
            'font/*',
            'fragment_program/*',
            'gui/**',
            'image/*',
            'input/*',
            'label/*',
            'light/*',
            'lua/*',
            'material/*',
            'mesh/*',
            'meshset/*',
            'misc/**',
            'model/*',
            'particlefx/*',
            'render/*',
            'render_script/*',
            'render_target/*',
            'resource/*',
            'script/*',
            'shader/*',
            'sound/*',
            'sprite/**',
            'sys/**',
            'texture/*',
            'textureset/*',
            'tile/*',
            'tilegrid/*',
            'vertex_program/*',
            'window/*']

    excl_pattern = ['**/*.ttf', '**/*.basis', '**/*.psd', '**/*.DS_Store']

    if not platform_supports_feature(bld.env.PLATFORM, 'compute', None):
        excl_pattern.append('**/*.cp')
        excl_pattern.append('**/*.compute')

    exported_symbols = ['ComponentTypeAnim',
                        'ComponentTypeGui',
                        'ComponentTypeMesh',
                        'ComponentTypeScript',
                        'ComponentTypeSound',
                        'ResourceProviderFile',
                        'ResourceTypeAnim',
                        'ResourceTypeAnimationSet',
                        'ResourceTypeCollection',
                        'ResourceTypeGameObject',
                        'ResourceTypeGui',
                        'ResourceTypeGuiScript',
                        'ResourceTypeLua',
                        'ResourceTypeOgg',
                        'ResourceTypeOpus',
                        'ResourceTypeScript',
                        'ResourceTypeTTF',
                        'ResourceTypeFont',
                        'ResourceTypeWav',
                        'GraphicsAdapterNull',
                        'ScriptImageExt',
                        'ScriptFont',
                        'ScriptHttp']

    embed_source = ['resource/booster_on_sfx.wav', 'resource/layer_guitar_a.ogg']

    dynamic_fonts = [
        'dyn_glyph_bank_test_1.font'
    ]

    test_gamesys = bld.program(features = 'cxx cprogram embed test',
                               includes = '../../../src ../../../proto .',
                               use = 'TESTMAIN DMGLFW GAMEOBJECT DDF RESOURCE PHYSICS RENDER FONT GRAPHICS_GAMESYS_TEST SOCKET APP PROFILE_NULL SCRIPT LUA EXTENSION INPUT PLATFORM_NULL HID_NULL PARTICLE RIG GUI SOUND_NULL LIVEUPDATE DLIB gamesys gamesys_rig gamesys_model',
                               exported_symbols = exported_symbols + ['ScriptModelExt'],
                               web_libs = ['library_sys.js', 'library_script.js', 'library_render.js'],
                               content_root='../test',
                               dynamic_fonts = dynamic_fonts,
                               source = bld.path.ant_glob('test_gamesys.cpp') + bld.path.ant_glob(dirs, excl=excl_pattern),
                               embed_source = embed_source,
                               target = 'test_gamesys')

    test_gamesys_physics = bld.program(features='cxx cprogram embed test',
                                includes='../../../src ../../../proto',
                                use='TESTMAIN DMGLFW GAMEOBJECT DDF RESOURCE PHYSICS RENDER FONT GRAPHICS_GAMESYS_TEST SOCKET APP PROFILE_NULL SCRIPT TEST_SCRIPT LUA EXTENSION INPUT PLATFORM_NULL HID_NULL PARTICLE RIG GUI SOUND_NULL LIVEUPDATE DLIB gamesys gamesys_rig gamesys_model',
                                exported_symbols = exported_symbols + ['ScriptModelExt'],
                                web_libs = ['library_sys.js', 'library_script.js', 'library_render.js'],
                                content_root='../test',
                                source=bld.path.ant_glob('test_gamesys_physics.cpp') + bld.path.ant_glob('collision_object/*'),
                                embed_source = embed_source,
                                target='test_gamesys_physics')

    if not 'web' in bld.env['PLATFORM']:
        test_gamesys_http = bld.program(features = 'cxx cprogram test',
                                        includes = '../../../src ../../../proto',
                                        use = 'TESTMAIN DMGLFW GAMEOBJECT DDF RESOURCE PHYSICS RENDER FONT GRAPHICS_GAMESYS_TEST SOCKET APP PROFILE_NULL SCRIPT TEST_SCRIPT LUA EXTENSION INPUT PLATFORM_NULL HID_NULL PARTICLE RIG GUI SOUND_NULL LIVEUPDATE DLIB gamesys gamesys_rig_null gamesys_model_null',
                                        exported_symbols = exported_symbols,
                                        source = bld.path.ant_glob('test_gamesys_http.cpp') + bld.path.ant_glob('http/*'),
                                        target = 'test_gamesys_http')

        if platform_supports_feature(bld.env.PLATFORM, 'test_script_http', {}):
            bld.add_group() # uses test data from http/* and we only want to build it once
            bld.program(features = 'cxx test',
                        includes = '../../../src ../../../proto .',
                        exported_symbols = exported_symbols,
                        use = 'TESTMAIN DMGLFW GAMEOBJECT DDF RESOURCE PHYSICS RENDER FONT GRAPHICS_NULL SOCKET APP PROFILE_NULL SCRIPT LUA EXTENSION INPUT PLATFORM_NULL HID_NULL PARTICLE RIG GUI SOUND_NULL LIVEUPDATE DLIB TEST_SCRIPT gamesys gamesys_rig_null gamesys_model_null',
                        source = ['test_script_http.cpp'],
                        target = 'test_script_http')

    if bld.env.PLATFORM in ('x86_64-win32', 'x86_64-macos', 'arm64-macos', 'x86_64-linux')  and not waflib.Options.options.with_vulkan:
        bld.recurse('fontview')
